from appium.webdriver.common.appiumby import AppiumBy
from AppTest.page.base_page import BasePage


class RosterPage(BasePage):
    __BUTTON_QUITPAGE = (AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/preceding-sibling::*")
    # 搜索
    __BUTTON_SEARCH = (AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/following::*[1]")
    __BUTTON_CANCEL = (AppiumBy.XPATH, "	//android.view.View[@content-desc=’取消‘]")
    __INPUT_SEARCH = (AppiumBy.XPATH, "//android.view.View[@content-desc='取消']/preceding-sibling::*")
    __BUTTON_SEARCH_RESULT = (
    AppiumBy.XPATH, "//android.view.View[@content-desc='取消']/following::*[1]/child::*/child::*/child::*")
    # 筛选
    __BUTTON_SELECT_ALL = (
        AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/parent::*/following::*[3]/child::*[1]")
    __BUTTON_SELECT_FULLTIME = (
        AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/parent::*/following::*[3]/child::*[2]")
    __BUTTON_SELECT_INTERNSHIP = (
        AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/parent::*/following::*[3]/child::*[3]")
    __BUTTON_SELECT_OTHER = (
        AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/parent::*/following::*[3]/child::*[4]")
    __BUTTON_SELECT_FORMAL = (
        AppiumBy.XPATH, "//android.view.View[@content-desc='​花​名​册​']/parent::*/following::*[3]/child::*[5]")
    # 添加在职员工
    __BUTTON_ADDEMPLOYEE = (AppiumBy.XPATH, "	//android.view.View[@content-desc='添加在职员工']")
    __INPUT_NAME = (AppiumBy.XPATH, "//android.view.View[@content-desc='姓名']/following-sibling::*[1]")
    __INPUT_PHONENUMBER = (
    AppiumBy.XPATH, "//android.view.View[@content-desc='手机']/following-sibling::*[1]/child::*[2]")
    __INPUT_SEX = (AppiumBy.XPATH, "//android.view.View[@content-desc='性别']/following-sibling::*[1]")
    __INPUT_WORKNUMBER = (AppiumBy.XPATH, "//android.view.View[@content-desc='工号']/following-sibling::*[1]")
    __INPUT_EMPLOYEETYPE = (AppiumBy.XPATH, "//android.view.View[@content-desc='员工类型']/following-sibling::*[1]")
    __INPUT_EMPLOYEEMENTDATE = (AppiumBy.XPATH, "//android.view.View[@content-desc='入职日期']/following-sibling::*[1]")
    __INPUT_DEPARTMENT = (AppiumBy.XPATH, "//android.view.View[@content-desc='部门']/following::*[1]")
    __INPUT_SUPERIOR = (AppiumBy.XPATH, "//android.view.View[@content-desc='直属上级']/following-sibling::*[1]")
    __BUTTON_SAVE = (AppiumBy.XPATH, "//android.widget.Button[@content-desc='保存']")
    __BUTTON_ADDANDSAVE = (AppiumBy.XPATH, "//android.widget.Button[@content-desc='保存并继续添加']")

    # 搜索框
    def search_input(self, keywords):
        self.do_click(self.__BUTTON_SEARCH)
        self.do_send_keys(self.__INPUT_SEARCH, keywords)
        try:
            return self.do_finds(self.__BUTTON_SEARCH_RESULT)
        except:
            return False

    def search_cancel(self):
        self.do_click(self.__BUTTON_CANCEL)

    # 类型筛选
    def select_all(self):
        self.do_click(self.__BUTTON_SELECT_ALL)

    def select_fulltime(self):
        self.do_click(self.__BUTTON_SELECT_FULLTIME)

    def select_internship(self):
        self.do_click(self.__BUTTON_SELECT_INTERNSHIP)

    def select_other(self):
        self.do_click(self.__BUTTON_SELECT_OTHER)

    def select_formal(self):
        self.do_click(self.__BUTTON_SELECT_FORMAL)

    # 添加成员
    def to_add(self):
        self.do_click(self.__BUTTON_ADDEMPLOYEE)

    def add_name(self, name):
        self.do_click(self.__INPUT_NAME)
        self.do_send_keys(self.__INPUT_NAME, name)

    def add_phonenumber(self, phonenumber):
        self.do_click(self.__INPUT_PHONENUMBER)
        self.do_send_keys(self.__INPUT_PHONENUMBER, phonenumber)

    def add_sex(self, sex):
        self.do_click(self.__INPUT_SEX)
        by = (AppiumBy.XPATH, f"//android.view.View[@content-desc='{sex}']")
        self.do_click(by)

    def add_worknumber(self, worknumber):
        self.do_click(self.__INPUT_WORKNUMBER)
        self.do_send_keys(self.__INPUT_WORKNUMBER, worknumber)

    def add_emptype(self, type):
        self.do_click(self.__INPUT_EMPLOYEETYPE)
        by = (AppiumBy.XPATH, f"//android.view.View[@content-desc='{type}']")
        self.do_click(by)

    def add_date(self,):
        self.do_click(self.__INPUT_EMPLOYEEMENTDATE)
        by = (AppiumBy.XPATH, "//android.widget.Button[@content-desc='确定']")
        self.do_click(by)

    def add_department(self, department):
        self.do_click(self.__INPUT_DEPARTMENT)
        by = (AppiumBy.XPATH, "//*[@text='选择部门']/parent::*/parent::*/parent::*/parent::*/parent::*/parent::*/child"
                              "::*[2]/child::*")
        self.do_find(by).click()
        by = (AppiumBy.XPATH, "//*[@text='确定']")
        self.do_click(by)

    def add_save(self):
        self.do_click(self.__BUTTON_SAVE)

    def add_saveadd(self):
        self.do_click(self.__BUTTON_ADDANDSAVE)


